<?php
class Member_positionModel extends Model {
	public function getList($map=array()){
		return $this->where($map)->select();
	}
	//更新部门管理员
	public function updateManager($data){
		$data=isset($data)?$data:$_POST;
		//先将员工表的部门管理员字段清除
		$memberDB=M('Member');		
		$memberDB->where(['memberpositionid'=>$data['member_position_id']])->setField('isPositionAdmin',0);
		//$memberDB->where(['id'=>$data['id']])->setField('isPositionAdmin',1);	
		//再更新职位表的记录
		if($data['isPositionAdmin']){
			$positionData['id']=$data['member_position_id'][0];
			$positionData['member_id']=$data['id'];
			$positionData['member_name']=$data['name'];
			$ret=$this->save($positionData);
		}
		return $ret;
	}
	public function getListToArray1($pid=0,$siteid=0,$deep=0,$arr=array()){
		$re=$this->where(array('pid'=>$pid,'site_id'=>$siteid))->order('sort')->select();
		if($re!==false){
			$deep++;
			foreach($re as $k=>$v){
				$v['deep']=$deep;
				$arr[]=$v;
				$arr=$this->getListToArray1($v['id'],$siteid,$deep,$arr);
			}
			return $arr;
		}
	}
	public function isLeader($memberid){
		$position=D('Member_position')->where('site_id ='.__SITEID__)->field('member_id')->select();
		$posarr=array();
		foreach($position as $key =>$val){//生成所有管理层 
			$temparr=explode(',',$val['member_id']);
			foreach($temparr as $k =>$v){
				if(!in_array($v,$posarr)&&!empty($v))
					array_push($posarr,$v);
			}
		}
		if(in_array($memberid,$posarr)){//判断是否为中间层级的管理者
			$membertype=1;
		}else{
			$membertype=0;
		}
		return $membertype;
	}

	public function setManager($mid,$gid){
		$data['member_name']=D('Member')->where('id ='.$mid)->getField('name');
		$data['member_id']=$mid;
		$state=$this->where('id ='.$gid)->save($data);
		if($state){
			return true;
		}else{
			return false;
		}
	}
	
}